package com.mpyy.handler.purchase;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mpyy.pojo.*;
import com.mpyy.service.purchase.PurchaseReturnServiceInter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @version 1.0
 * @Description PurchaseReturnHandler
 * @Author liangxinyu
 * @Date 2022-03-09 14:08
 */
@Controller
public class PurchaseReturnHandler {
    @Autowired
    @Qualifier(value = "purchaseReturnServiceImpl")
    private PurchaseReturnServiceInter purchaseReturnServiceInter;


    /* --------------------------------------------purchase_return.html-------------------------------------------- */

    /* 供货商信息查询 */
    @RequestMapping(value = {"/searchSupplierInfo"})
    @ResponseBody
    public String searchSupplierInfo(){
        List<SupplierInfo> supplierInfos = purchaseReturnServiceInter.searchSupplierInfo();
        HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("code",0);
        objectObjectHashMap.put("msg","");
        objectObjectHashMap.put("count",supplierInfos.size());
        objectObjectHashMap.put("data",supplierInfos);
        return JSON.toJSONString(objectObjectHashMap);
    }

    /* 出货仓库 */
    @RequestMapping(value = {"/purchaseReturn"})
    @ResponseBody
    public String searchDepotInfo(){
        List<DepotInfo> depotInfos = purchaseReturnServiceInter.searchDepotInfo();
        return JSON.toJSONString(depotInfos);
    }

    /* 供货商信息查询 */
    @RequestMapping(value = {"/searchSupplierType"})
    @ResponseBody
    public String searchSupplierType(){
        List<AddSupplierType> addSupplierTypes = purchaseReturnServiceInter.searchSupplierType();
        HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("code",0);
        objectObjectHashMap.put("msg","");
        objectObjectHashMap.put("count",addSupplierTypes.size());
        objectObjectHashMap.put("data",addSupplierTypes);
        return JSON.toJSONString(objectObjectHashMap);
    }

    /* 供货商类型 + 默认选择 */
    @RequestMapping(value = {"/doUpdateSupplierType"},method = RequestMethod.PUT)
    @ResponseBody   // 如果要返回数据到页面就need这个注解
    public void doUpdateSupplierType(String supType,String defaultSetting){
        AddSupplierType addSupplierType = new AddSupplierType();
        addSupplierType.setSupType("101" + supType);
        addSupplierType.setSupType(supType);
        if(defaultSetting == null){
            addSupplierType.setDefaultSetting(0);
        }else{
            addSupplierType.setDefaultSetting(1);
        }
        int i = purchaseReturnServiceInter.doUpdateSupplierType(addSupplierType);
    }

    /* 经办人 */
    @RequestMapping(value = {"/searchHandler"})
    @ResponseBody
    public String searchHandler(){
        List<EmployeeInfo> employeeInfos = purchaseReturnServiceInter.searchHandler();
        return JSON.toJSONString(employeeInfos);
    }

    /* 生成采购退货单、采购退货详情单、修改库存明细表、新增库存变动 */
    @RequestMapping(value = {"/doUpdateMultipurpose/{purchaseReturnSingle}/{purchaseReturnDesc}"})
    @ResponseBody
    public String doUpdateMultipurpose(@PathVariable String purchaseReturnSingle, @PathVariable String purchaseReturnDesc) throws Exception {
        //实例化一个商品采购退货对象
        PurchaseReturn purchaseReturn = new PurchaseReturn();
        //获取页面传递过来的采购退货数据，并转成pojo对象
        JSONObject pr = JSONObject.parseObject(purchaseReturnSingle);
        purchaseReturn.setDepotId(pr.getString("depotId"));
        purchaseReturn.setEmpId(pr.getString("empId"));
        purchaseReturn.setPurRetComment(pr.getString("purRetComment"));
        purchaseReturn.setPurRetDate(pr.getString("purRetDate"));
        purchaseReturn.setPurRetId(pr.getString("purRetId"));
        purchaseReturn.setPurRetRealRefund(pr.getDouble("purRetRealRefund"));
        purchaseReturn.setPurRetRefund(pr.getDouble("purRetRefund"));
        purchaseReturn.setSupId(pr.getString("supId"));

        //创建一个StringBuffer对象，获取页面传递过来的销售订单明细数据，拼接还原后加入StringBuffer中
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("[" + purchaseReturnDesc + "]");

        //调用自定义的转换工具类
        List<PurchaseStockTemporary> purchaseStockTemporaries = ProSalesUtil.<PurchaseStockTemporary>decomposeJsonArray(stringBuffer, PurchaseStockTemporary.class);

        /* 定义-采购退货详情单  集合 */
        List<PurchaseReturnDesc> purchaseReturnDescs = new ArrayList<>();

        /* 定义-库存明细表  集合 */
        List<InventoryDesc> inventoryDescs = new ArrayList<>();


        /* 定义-库存变动  集合 */
        List<InventoryChange> inventoryChanges = new ArrayList<>();


        for(PurchaseStockTemporary purchaseStockTemporary:purchaseStockTemporaries){

            /* 定义-采购退货详情单  对象 */
            PurchaseReturnDesc purchaseReturnDesc1 = new PurchaseReturnDesc();
            purchaseReturnDesc1.setPurRetDescId(purchaseStockTemporary.getPurRetDescId());
            purchaseReturnDesc1.setPurRetId(purchaseReturn.getPurRetId());
            purchaseReturnDesc1.setProId(purchaseStockTemporary.getProId());
            purchaseReturnDesc1.setProName(purchaseStockTemporary.getInveDescName());
            purchaseReturnDesc1.setPurRetDescPrice(purchaseStockTemporary.getPurStoDescPrice());
            purchaseReturnDesc1.setProBatchNum(purchaseStockTemporary.getInveDescBatchNum());
            purchaseReturnDesc1.setPurRetDescNum(purchaseStockTemporary.getReturnQuantity());
            purchaseReturnDesc1.setPurRetDescComment("");
            purchaseReturnDesc1.setPurRetDescUnit(purchaseStockTemporary.getInveDescUnit());
            purchaseReturnDesc1.setPurRetDescAgentiaTypeName("");
            purchaseReturnDesc1.setPurRetDescManufacturer(purchaseStockTemporary.getInveDescManufacturer());

            /* 存入集合中 */
            purchaseReturnDescs.add(purchaseReturnDesc1);


            /* 定义-库存明细表  对象 */
            InventoryDesc inventoryDesc = new InventoryDesc();
            inventoryDesc.setProId(purchaseStockTemporary.getProId());
            inventoryDesc.setInveDescBatchNum(purchaseStockTemporary.getInveDescBatchNum());
            inventoryDesc.setInveDescInventoryNum(purchaseStockTemporary.getReturnQuantity());
            inventoryDesc.setInveDescDepot(purchaseReturn.getDepotId());
            inventoryDesc.setSupId(purchaseReturn.getSupId());

            /* 存入集合中 */
            inventoryDescs.add(inventoryDesc);


            /* 定义-库存变动  对象 */
            InventoryChange inventoryChange = new InventoryChange();
            inventoryChange.setInvTurnId(0);
            inventoryChange.setProId(purchaseStockTemporary.getProId());
            inventoryChange.setInvTurnProName(purchaseStockTemporary.getInveDescName());
            inventoryChange.setInvUnitName(purchaseStockTemporary.getInveDescUnit());
            inventoryChange.setInvTurnManufacturer(purchaseStockTemporary.getInveDescManufacturer());
            inventoryChange.setInvTurnModel(purchaseStockTemporary.getInveDescSize());
            inventoryChange.setInvTurnBatchNum(purchaseStockTemporary.getInveDescBatchNum());
            inventoryChange.setInvPurchaseReturnNum(purchaseStockTemporary.getReturnQuantity());
            inventoryChange.setInvTurnDate(purchaseReturn.getPurRetDate());

            /* 存入集合中 */
            inventoryChanges.add(inventoryChange);

        }

        // 调用新增商品销售信息的方法
        boolean b = purchaseReturnServiceInter.doUpdateMultipurpose(purchaseReturn,purchaseReturnDescs,inventoryDescs,inventoryChanges);
        return JSON.toJSONString(b);
    }



    /* --------------------------------------------add_commodity.html-------------------------------------------- */

    /* 添加退货商品按钮 */  /* 商品批次信息 数据表   查询 */
    @RequestMapping(value = {"/addReturnedInfoBtn"})
    @ResponseBody
    public String addReturnedInfoBtn(String depotId,String supId,String proId,String signProList,String idOrName,String proTypeId){
        List<InventoryDesc> inventoryDescs = purchaseReturnServiceInter.addReturnedInfoBtn(depotId,supId,proId,signProList,idOrName,proTypeId);
        HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("code",0);
        objectObjectHashMap.put("msg","");
        objectObjectHashMap.put("count",inventoryDescs.size());
        objectObjectHashMap.put("data",inventoryDescs);
        return JSON.toJSONString(objectObjectHashMap);
    }

    /* 根据编号或名称查询商品 */
    @RequestMapping(value = {"/seachGoodsByNumberOrName/{idOrName}"})
    @ResponseBody
    public String seachGoodsByNumberOrName(@PathVariable String idOrName){

        return "";
    }

    /* --------------------------------------------原始单号-------------------------------------------- */
    @RequestMapping(value = {"/seachOriginalOrderNumber"})
    @ResponseBody
    public String seachOriginalOrderNumber(String startDate,String endDate){
        List<PurchaseStock> purchaseStocks = purchaseReturnServiceInter.seachOriginalOrderNumber(startDate, endDate);
        return JSON.toJSONString(purchaseStocks);
    }

    /* 根据进货单号   搜索商品 */
    @RequestMapping(value = {"/seachToCommodityByOrder"})
    @ResponseBody
    public String seachToCommodityByOrder(String purStoId){
        List<PurchaseStockDesc> purchaseStockDescs = purchaseReturnServiceInter.seachToCommodityByOrder(purStoId);
        return JSON.toJSONString(purchaseStockDescs);
    }
}
